package cn.jekun.web.agent.service;

import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import oracle.jdbc.driver.SQLUtil;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.jekun.component.base.bean.Page;
import cn.jekun.component.base.utils.SqlUtis;
import cn.jekun.component.base.validator.annotation.NotNull;
import cn.jekun.component.core.bean.gen.ErpShop;
import cn.jekun.component.core.bean.gen.RsAgent;
import cn.jekun.component.core.bean.gen.DCity;
import cn.jekun.component.core.bean.gen.DProvince;
import cn.jekun.component.core.bean.gen.DRegion;
import cn.jekun.component.core.service.gen.ErpShopService;
import cn.jekun.component.core.service.gen.RsAgentService;
import cn.jekun.web.agent.bean.Agent;
import cn.jekun.web.agent.bean.ShopMemberConcern;

/**
 * 账户流水查询Service
 * 
 */
@Service(WebAgentService.BEAN_NAME)
public class WebAgentService extends AgentBaseService {
	public static final String BEAN_NAME = "cn.jekun.web.agent.service.WebAgentService";

	@Resource(name = RsAgentService.BEAN_NAME)
	RsAgentService rsAgentService;

	@Resource(name = ErpShopService.BEAN_NAME)
	ErpShopService erpShopService;

	
	@Override
	public String getNamespace() {
		// TODO Auto-generated method stub
		return "ACTION_AGENT";
	}

	/**
	 * update
	 * 
	 * 根据AgentId获取代理商
	 * 
	 * @return
	 */
	public Agent getRsAgent(@RsAgent.AnnoAgentId String agentId) {
		Map formMap = new HashMap();
		formMap.put(RsAgent.COL_AGENT_ID, agentId);
		SqlUtis.prepareSql(formMap, SqlUtis.getSQL(formMap, "a." + RsAgent.COL_AGENT_ID, Types.VARCHAR, SqlUtis.EQ));
		return this.one(this.getStatementName("getAgentDetailsById"),formMap);
	}

	/**
	 * 根据代理商代码，查看此代理商是否存在
	 * 
	 * @return
	 */
	public boolean exists(@RsAgent.AnnoAgentId String agentId) {
		Map formMap = new HashMap();
		formMap.put(RsAgent.COL_AGENT_ID, agentId);
		SqlUtis.prepareSql(formMap, SqlUtis.getSQL(formMap, "a." + RsAgent.COL_AGENT_ID, Types.VARCHAR, SqlUtis.EQ));
		return this.rsAgentService.exists(formMap);
	}
	
	/**
	 * 根据代理商手机号码，查看此代理商是否存在
	 * 
	 * @return
	 */
	public boolean existsByPhone(@RsAgent.AnnoMobile String mobile) {
		Map formMap = new HashMap();
		formMap.put(RsAgent.COL_MOBILE, mobile);
		SqlUtis.prepareSql(formMap, SqlUtis.getSQL(formMap, "a." + RsAgent.COL_MOBILE, Types.VARCHAR, SqlUtis.EQ));
		return this.rsAgentService.exists(formMap);
	}
	
	/**
	 * 根据代理商的名字
	 * @param agentName
	 * @return
	 */
	public boolean existsByAgentName(String agentName){
		Map formMap = new HashMap();
		formMap.put(RsAgent.COL_AGENT_NAME,agentName);
	    SqlUtis.prepareSql(formMap, SqlUtis.getSQL(formMap, "a." + RsAgent.COL_AGENT_NAME,Types.VARCHAR,SqlUtis.EQ));
	    return this.rsAgentService.exists(formMap);
	}
	
	

	/**
	 * 更新代理商基本信息
	 * 
	 * @param agent
	 */
	@Transactional
	public void updateAgent(@NotNull(message = "代理商对象不为空") RsAgent agent, @RsAgent.AnnoAgentId String agentId) {
		Map formMap = new HashMap();
		formMap.put("rsAgent", agent);
		formMap.put(RsAgent.COL_AGENT_ID, agentId);
		SqlUtis.prepareSql(formMap, SqlUtis.getSQL(formMap, "a." + RsAgent.COL_AGENT_ID, Types.VARCHAR, SqlUtis.EQ));
		this.rsAgentService.update(formMap);
	}

	/**
	 * 获取商家的列表
	 * 
	 * @return
	 */

	public List<Agent> getErpShopList(@ErpShop.AnnoAgentId String agentId, @ErpShop.AnnoShopId String shopId, @ErpShop.AnnoShopName String shopName, Page page) {
		Map formMap = new HashMap();
		formMap.put(ErpShop.COL_AGENT_ID, agentId);
		formMap.put(ErpShop.COL_SHOP_ID, shopId);
		formMap.put(ErpShop.COL_SHOP_NAME, shopName);
		SqlUtis.prepareSql(formMap, SqlUtis.getSQL(formMap, "a." + ErpShop.COL_AGENT_ID, Types.VARCHAR, SqlUtis.EQ), SqlUtis.getSQL(formMap, "a." + ErpShop.COL_SHOP_ID, Types.VARCHAR, SqlUtis.LIKE),
				SqlUtis.getSQL(formMap, "a." + ErpShop.COL_SHOP_NAME, Types.VARCHAR, SqlUtis.LIKE));
		return this.list("ACTION_AGENT.getErpShopList",formMap, page);
	}

	/**
	 * 获取商家的列表详情
	 * 
	 * @return
	 */
	public List getErpShopList(@ErpShop.AnnoAgentId String agentId, @ErpShop.AnnoShopName String shopName, String provinceId, String cityId, String regionId, Page page) {
		Map formMap = new HashMap();
		formMap.put(RsAgent.COL_AGENT_ID, agentId);
		formMap.put(ErpShop.COL_SHOP_NAME, shopName);
		formMap.put(DProvince.COL_PROVINCE_ID, provinceId);
		formMap.put(DCity.COL_CITY_ID, cityId);
		formMap.put(DRegion.COL_REGION_ID, regionId);
		SqlUtis.prepareSql(formMap, SqlUtis.getSQL(formMap, "a." + RsAgent.COL_AGENT_ID, Types.VARCHAR, SqlUtis.EQ),
				SqlUtis.getSQL(formMap, "a." + DProvince.COL_PROVINCE_ID, Types.VARCHAR, SqlUtis.EQ), SqlUtis.getSQL(formMap, "a." + DCity.COL_CITY_ID, Types.VARCHAR, SqlUtis.EQ),
				SqlUtis.getSQL(formMap, "a." + DRegion.COL_REGION_ID, Types.VARCHAR, SqlUtis.EQ), SqlUtis.getSQL(formMap, "a." + ErpShop.COL_SHOP_NAME, Types.VARCHAR, SqlUtis.LIKE));
		return this.erpShopService.list(formMap, page);
	}

//	public List<String> queryTerminalsByShopId(@RsTerminal.AnnoShopId String shopId) {
//		return this.rsTerminalService.listBySql("select a.terminal_id from rs_terminal a where a.shop_id='" + shopId + "'");
//	}

	/**
	 * 获取代理商的下属代理商、运营商
	 * 
	 * @return
	 */
	public List<RsAgent> getAgentListByParentId(@RsAgent.AnnoParentAgentId String parentId) {
		Map formMap = new HashMap();
		formMap.put(RsAgent.COL_PARENT_AGENT_ID, parentId);
		SqlUtis.prepareSql(formMap, SqlUtis.getSQL(formMap, "a." + RsAgent.COL_PARENT_AGENT_ID, Types.VARCHAR, SqlUtis.EQ));
		return this.rsAgentService.list(formMap);
	}
	
	

	/**
	 * 獲取代理商的商店
	 * 
	 * @param agentId
	 * @return
	 */
	public List<ErpShop> getShopListByAgentId(@ErpShop.AnnoAgentId String agentId) {
		Map formMap = new HashMap();
		formMap.put(ErpShop.COL_AGENT_ID, agentId);
		SqlUtis.prepareSql(formMap, SqlUtis.getSQL(formMap, "a." + ErpShop.COL_AGENT_ID, Types.VARCHAR, SqlUtis.EQ));
		return this.erpShopService.list(formMap);
	}
	
	public ShopMemberConcern getShopMemberConcern(@ErpShop.AnnoShopId String shopId){
		
		ShopMemberConcern  shopMemberConcern = null;
//		System.out.println(shopMemberConcern.getConcernNum() + " :: " + shopMemberConcern.getMemberNum());
		Map hashMap = new HashMap<String, String>();
		hashMap.put("shopId", shopId);
		shopMemberConcern  = this.one(getStatementName("getMerConNum"),hashMap);
		if(shopMemberConcern == null){
			shopMemberConcern = new ShopMemberConcern();
		}
		return shopMemberConcern;
	}

//	/**
//	 * 獲取代理商的商家的終端
//	 * 
//	 * @return
//	 */
//	public List<RsTerminal> getTerminalListByShopId(@RsTerminal.AnnoShopId String shopId) {
//		Map formMap = new HashMap();
//		formMap.put(RsTerminal.COL_SHOP_ID, shopId);
//		SqlUtis.prepareSql(formMap, SqlUtis.getSQL(formMap, "a." + RsTerminal.COL_SHOP_ID, Types.VARCHAR, SqlUtis.EQ));
//		return this.rsTerminalService.list(formMap);
//		}
//	
//	/**
//	 *  根据代理商代码查找终端
//	 *  @return
//	 *
//	 */
//	public List<RsAgentTerminal> getTerminalByAgent(@RsAgentTerminal.AnnoAgentId String agentId){
//		Map hashMap = new HashMap<String, String>();
//		hashMap.put(RsAgentTerminal.COL_AGENT_ID, agentId);
//		SqlUtis.prepareSql(hashMap, SqlUtis.getSQL(hashMap, "a."+RsAgentTerminal.COL_AGENT_ID,Types.VARCHAR,SqlUtis.EQ));
//		return rsAgentTerminalService.list(hashMap);
//	}

	// private TreeAgent treeAgent = new TreeAgent();
	// private JSONObject jsonObject = new JSONObject();

//	/**
//	 * 动态生成属性结构
//	 * @param agentId //代理商的代码
//	 * @return
//	 */
//	public TreeAgent agentNote(@RsAgent.AnnoAgentId String agentId) {
//		TreeAgent treeAgent = null;
//		RsAgent agent = this.getRsAgent(agentId);  //根据代理商代码获取代理商
//		if (agent != null) {  
//			treeAgent = new TreeAgent(); //创建代理商
//			treeAgent.setAgentId(agent.getAgentId());  //设置树形代理商的代码
//			treeAgent.setAgentName(agent.getAgentName());  //设置属性代理商的名称
//			treeAgent.setAgnetType(agent.getAgentType()); //代理商的类型
//			treeAgent.setAgentParentId(agent.getParentAgentId()); //此代理商的上级代理商
//		}
//		List<ErpShop> shopList = this.getShopListByAgentId(agentId); //根据代理商代码，获取商家的
//		List<RsAgent> rsAgentList = this.getAgentListByParentId(agentId);  //根据代理商代码查找下一级的代理商
//		List<TreeShop> treeShops = new ArrayList<TreeShop>();
//		List<RsTerminal> terminals = null;
//		for (ErpShop shop : shopList) {
//			TreeShop treeShop = new TreeShop();
//			String shopId = shop.getShopId();
//			treeShop.setAgentId(shop.getAgentId());
//			treeShop.setShopId(shopId);
//			treeShop.setShopName(shop.getShopName());
//			terminals = this.getTerminalListByShopId(shopId);
//			for (RsTerminal terminal : terminals) {
//				TreeTerminal t = new TreeTerminal();
//				t.setShopId(terminal.getShopId());
//				t.setTerminalId(terminal.getTerminalId());
//				treeShop.getTerminalList().add(t);
//			}
//			treeShops.add(treeShop);
//		}
//		for (RsAgent childAgent : rsAgentList) {
//			TreeAgent ta = this.agentNote(childAgent.getAgentId());
//			treeAgent.getAgentList().add(ta);
//		}
//		treeAgent.setShopList(treeShops);
//		return treeAgent;
//	}
	
	/**
	 * 获取子代理商的数量
	 * @param agentId
	 * @return
	 */
	public int getChildAgentByParentId(@RsAgent.AnnoAgentId String agentId){
		Map formMap = new HashMap();
		formMap.put(RsAgent.COL_PARENT_AGENT_ID, agentId);
		SqlUtis.prepareSql(formMap, SqlUtis.getSQL(formMap, "a." + RsAgent.COL_PARENT_AGENT_ID, Types.VARCHAR, SqlUtis.EQ));
		return this.rsAgentService.count(formMap);
	}
//	/**
//	 * 获取商户的数量
//	 * @param shopId
//	 * @return
//	 */
//	public int getChildTerminalByShopId(@ErpShop.AnnoShopId String shopId){
//		Map formMap =  new HashMap();
//		formMap.put(RsTerminal.COL_SHOP_ID, shopId);
//		SqlUtis.prepareSql(formMap, SqlUtis.getSQL(formMap,"a." + RsTerminal.COL_SHOP_ID, Types.VARCHAR, SqlUtis.EQ));
//        return this.rsTerminalService.count(formMap);
//	}
	
	/**
	 *  根据代理商的代码查找商家的数量
	 *  @param agentId
	 *  @return
	 *
	 */
	public int getChildShopByAgentId(@ErpShop.AnnoAgentId String agentId){
		
		Map formMap =  new HashMap();
		formMap.put(ErpShop.COL_AGENT_ID, agentId);
		SqlUtis.prepareSql(formMap, SqlUtis.getSQL(formMap,"a." + ErpShop.COL_AGENT_ID, Types.VARCHAR, SqlUtis.EQ));
        return this.erpShopService.count(formMap);
	}
	
}
